package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.pojo.RealtimeDataEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ADA_HealthAirAdapter extends RecyclerView.Adapter<ADA_HealthAirAdapter.AirViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<RealtimeDataEntity.AirBean> mList;

    public ADA_HealthAirAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void setDataList(List<RealtimeDataEntity.AirBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public AirViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AirViewHolder(mInflater.inflate(R.layout.item_health_list, null, false));
    }

    @Override
    public void onBindViewHolder(ADA_HealthAirAdapter.AirViewHolder holder, final int position) {
        final RealtimeDataEntity.AirBean bean = mList.get(position);
        //1 PM2.5,2 温度,3 湿度,4 甲醛
        String text = "";
        switch (bean.getType()) {
            case "1":
                text = "PM2.5";
                Glide.with(mContext).load(R.drawable.healthdata_icon_pm).into(holder.ivItemImg);
                break;
            case "2":
                Glide.with(mContext).load(R.drawable.healthdata_icon_sun).into(holder.ivItemImg);
                text = "温度";
                break;
            case "3":
                Glide.with(mContext).load(R.drawable.healthdata_icon_humidity).into(holder.ivItemImg);
                text = "湿度";
                break;
            case "4":
                Glide.with(mContext).load(R.drawable.healthdata_icon_formaldehyde).into(holder.ivItemImg);
                text = "甲醛";
                break;
        }
        holder.tvItemName.setText(text);
        holder.tvItemGrade.setText(bean.getStatus());
        holder.tvItemNumber.setText(bean.getNum());

        holder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClicked(Integer.parseInt(bean.getType()));
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class AirViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_item_root)
        LinearLayout llRoot;
        @BindView(R.id.tv_item_name)
        TextView tvItemName;
        @BindView(R.id.iv_item_img)
        ImageView ivItemImg;
        @BindView(R.id.tv_item_grade)
        TextView tvItemGrade;
        @BindView(R.id.tv_item_number)
        TextView tvItemNumber;

        public AirViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClicked(int feature);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }
}
